<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.artNo" placeholder="货号" clearable style="width: 130px; margin-top: 8px;" class="filter-item" />
      <el-select v-model="listQuery.brandId" placeholder="品牌" clearable filterable style="width: 140px; margin-left: 10px; margin-top: 8px;" class="filter-item">
        <el-option v-for="item in brandOptions" :key="item.key" :label="item.display_name" :value="item.key" />
      </el-select>
      <el-input v-model="listQuery.titleKeyword" placeholder="预售单据" clearable style="width: 130px; margin-left: 10px; margin-top: 8px;" class="filter-item" />
      <el-date-picker
        v-model="expectRecivedDate"
        type="daterange"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        range-separator="至"
        start-placeholder="预计到货日期"
        end-placeholder="预计到货日期"
        style="margin-left: 10px;"
      />
      <el-button v-waves class="filter-item" style="margin-left: 10px; margin-top: 8px;" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button v-waves class="filter-item" style="margin-left: 10px; margin-top: 8px;" type="info" icon="el-icon-refresh" @click="handleRefresh">
        重置
      </el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" :header-cell-style="headerCellStyle" :cell-style="{padding:'0px'}" border stripe element-loading-text="拼命加载中">
      <el-table-column label="预计到货日期" align="center" min-width="110px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.expectRecivedDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预售单据" align="center" min-width="110px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="其它概况" align="center" min-width="110px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="110px">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleDetail(row.id)">
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { fetchEnum } from '@/api/system-api'
import { getBookingByPage } from '@/api/distributor/presale-api'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { toOptionsList, toKeyValueMap } from '@/utils/index'

var brandKeyValue = {}

export default {
  name: 'DistributorPresaleBooking',
  components: { Pagination },
  directives: { waves },
  filters: {
    brandFilter(brandId) {
      return brandKeyValue[brandId]
    }
  },
  data() {
    return {
      brandOptions: [],
      expectRecivedDate: [],
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        artNo: null,
        brandId: null,
        titleKeyword: null,
        expectRecivedDateFrom: null,
        expectRecivedDateTo: null
      },

      listLoading: true,
      list: null,
      total: 0

    }
  },
  async created() {
    await this.initLoad()
    await this.getList()
  },
  methods: {
    async initLoad() {
      const enumCodes = ['BRAND']
      const { data } = await fetchEnum(enumCodes)
      for (const e of data) {
        if (e.code === 'BRAND') {
          if (e.options) {
            this.brandOptions = toOptionsList(e.options, 'id', 'shortName')
            brandKeyValue = toKeyValueMap(this.brandOptions, 'key', 'display_name')
          }
        }
      }
      this.initListQuery()
    },
    getList() {
      this.listLoading = true
      if (this.expectRecivedDate && this.expectRecivedDate.length === 2) {
        this.listQuery.expectRecivedDateFrom = this.expectRecivedDate[0]
        this.listQuery.expectRecivedDateTo = this.expectRecivedDate[1]
      } else {
        this.listQuery.expectRecivedDateFrom = null
        this.listQuery.expectRecivedDateTo = null
      }
      getBookingByPage(this.listQuery).then(response => {
        const { data } = response
        this.list = data.result
        this.total = data.totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 1 * 1000)
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
        this.listLoading = false
      })
    },
    initListQuery() {
      this.listQuery = {
        pageNo: 1,
        pageSize: 20,
        artNo: null,
        brandId: null,
        titleKeyword: null,
        expectRecivedDateFrom: null,
        expectRecivedDateTo: null
      }
    },

    handleFilter() {
      this.getList()
    },
    handleRefresh() {
      this.initListQuery()
      this.getList()
    },
    headerCellStyle({ row, rowIndex }) {
      return 'background-color: #42b983; color: #FFF;'
    },

    handleDetail(id) {
    }

  }
}

</script>
